package com.knife.autoconfigure.mybatisplus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.knife.mybatisplus.extension.config.KnifeMybatisPlusProperties;
import com.knife.mybatisplus.extension.plugins.handler.UserLineHandler;
import com.knife.mybatisplus.extension.plugins.inner.UserLineInnerInterceptor;
import com.knife.oauth.tenant.aspect.KnifeTenantAdvice;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author tianhao.luo@hand-china.com
 * @version 1.0
 * @date 2022年02月13日 星期日 2:29 下午
 * @description
 * @since 1.0
 */
@Configuration
@ComponentScan(basePackages = {"com.knife.mybatisplus"})
@EnableConfigurationProperties(value = KnifeMybatisPlusProperties.class)
public class MybatisMapperAutoConfiguration {

    /**
     * @description 配置mapper扫描
     * @since 1.0
     * @version 1.0
     * @author tianhao.luo@hand-china.com
     * @date 2022/2/13 2:32 下午
     * @return
     */
    @Bean
    @Primary
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("*.**.mapper");
        return mapperScannerConfigurer;
    }

    /**
     * @description
     * @since 1.0
     * @version 1.0
     * @author tianhao.luo@hand-china.com
     * @date 2022/2/15 10:58 上午
     * @return 配置mybatis-plus的拦截器
     */
    @Bean
    @Primary
    public MybatisPlusInterceptor mybatisPlusInterceptor(KnifeMybatisPlusProperties knifeMybatisPlusProperties) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new UserLineInnerInterceptor(true, new UserLineHandler() {
            @Override
            public boolean ignoreTable(String tableName) {
                return false;
            }
        }));
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                return new LongValue(KnifeTenantAdvice.getTenantId());
            }

            // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
            @Override
            public boolean ignoreTable(String tableName) {
                return knifeMybatisPlusProperties.getTenantIgnoreTables().contains(tableName);
            }
        }));
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
